package com.kifas.controller;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriInfo;
import javax.xml.bind.JAXBElement;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.kifas.entity.SupplierEntity;
import com.kifas.model.xml.PersistStatus;
import com.kifas.model.xml.SupplierModel;
import com.kifas.service.EntityServiceManager;

@Component
@Scope("prototype")
@Path("/Suppliers")
public class SupplierController {

	private static Logger log = Logger.getLogger(SupplierController.class);

	@Context
	UriInfo uriInfo;

	@Autowired
	private EntityServiceManager entityManager;

	@GET
	@Produces(MediaType.TEXT_XML)
	public List<SupplierModel> getSuppliers() {
		return transformList(entityManager.getSuppliers());
	}

	@GET
	@Path("{id}")
	@Produces(MediaType.TEXT_XML)
	public SupplierModel getSupplier(@PathParam("id") Long idSupplier) {
		return transform(entityManager.getSupplier(idSupplier));
	}

	@POST
	@Consumes(MediaType.APPLICATION_XML)
	@Produces(MediaType.APPLICATION_XML)
	public PersistStatus insert(JAXBElement<SupplierModel> xml) {
		SupplierModel model = xml.getValue();
		SupplierEntity supplier = new SupplierEntity();
		supplier.setIdSupplier(null);
		supplier.setNamaSupplier(model.getNamaSupplier());
		supplier.setAlamat(model.getAlamat());
		supplier.setNoTelp(model.getNoTelp());
		supplier.setNoFax(model.getNoFax());
		supplier.setContactPerson(model.getContactPerson());
		try {
			entityManager.persist(supplier);
		} catch (Exception e) {
			log.error("can not persist supplier", e);
			return new PersistStatus("SupplierEntity", "failed", "can not persist supplier");
		}
		return new PersistStatus("SupplierEntity", "success");
	}

	@PUT
	@Consumes(MediaType.APPLICATION_XML)
	@Produces(MediaType.APPLICATION_XML)
	public PersistStatus update(JAXBElement<SupplierModel> xml) {
		SupplierModel model = xml.getValue();
		SupplierEntity supplier = new SupplierEntity();
		supplier.setIdSupplier(model.getIdSupplier());
		supplier.setNamaSupplier(model.getNamaSupplier());
		supplier.setAlamat(model.getAlamat());
		supplier.setNoTelp(model.getNoTelp());
		supplier.setNoFax(model.getNoFax());
		supplier.setContactPerson(model.getContactPerson());
		try {
			entityManager.update(supplier);
		} catch (Exception e) {
			log.error("can not update supplier", e);
			return new PersistStatus("SupplierEntity", "failed", "can not update supplier");
		}
		return new PersistStatus("SupplierEntity", "success");
	}

	private List<SupplierModel> transformList(List<SupplierEntity> suppliers) {
		List<SupplierModel> supplierModels = new ArrayList<SupplierModel>();
		for (SupplierEntity supplier : suppliers) {
			supplierModels.add(transform(supplier));
		}
		return supplierModels;
	}

	private SupplierModel transform(SupplierEntity supplier) {
		return new SupplierModel(supplier.getIdSupplier(), supplier.getNamaSupplier(), supplier.getAlamat(), supplier.getNoTelp(), supplier.getNoFax(),
				supplier.getContactPerson(), supplier.getCreatedBy());
	}

}
